bitkeeper revision 1.1327.2.12 (4270a5f3EYQTq9cfFkGDYjjJ7ckjuw)
authormjw@wray-m-3.hpl.hp.com <mjw@wray-m-3.hpl.hp.com>
Thu, 28 Apr 2005 08:59:31 +0000 (08:59 +0000)
committermjw@wray-m-3.hpl.hp.com <mjw@wray-m-3.hpl.hp.com>
Thu, 28 Apr 2005 08:59:31 +0000 (08:59 +0000)
Fix problem with Python 2.4 caused by change in error behaviour
of dircache.listdir. In 2.4 it throws an error on non-existent
directories instead of returning an empty list.
See 'Porting to 2.4' in the Python release notes.

Signed-off-by: Mike Wray <mike.wray@hp.com>
tools/python/xen/xend/XendDB.py

index 6a27e65b5828f8fb5fdb81f8ed93792a4d819174..1701b5183b1ad01e2036a83abf55f90198f19fc0 100644 (file)
@@ -20,6 +20,12 @@ class XendDB:
             self.dbpath = os.path.join(self.dbpath, path)
         pass
 
+    def listdir(self, dpath):
+        try:
+            return dircache.listdir(dpath)
+        except:
+            return []
+
     def filepath(self, path):
         return os.path.join(self.dbpath, path)
         
@@ -52,21 +58,37 @@ class XendDB:
         return self.savefile(fpath, sxpr)
     
     def savefile(self, fpath, sxpr):
+        backup = False
         fdir = os.path.dirname(fpath)
         if not os.path.isdir(fdir):
             os.makedirs(fdir)
+        if os.path.exists(fpath):
+            backup = True
+            real_fpath = fpath
+            fpath += ".new."
+            
         fout = file(fpath, "wb+")
         try:
-            t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
-            fout.write("# %s %s\n" % (fpath, t))
-            sxp.show(sxpr, out=fout)
-        finally:
-            fout.close()
+            try:
+                t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
+                fout.write("# %s %s\n" % (fpath, t))
+                sxp.show(sxpr, out=fout)
+            finally:
+                fout.close()
+        except:
+            if backup:
+                try:
+                    os.unlink(fpath)
+                except:
+                    pass
+                raise
+        if backup:
+            os.rename(fpath, real_fpath)
 
     def fetchall(self, path):
         dpath = self.filepath(path)
         d = {}
-        for k in dircache.listdir(dpath):
+        for k in self.listdir(dpath):
             try:
                 v = self.fetchfile(os.path.join(dpath, k))
                 d[k] = v
@@ -84,8 +106,7 @@ class XendDB:
 
     def ls(self, path):
         dpath = self.filepath(path)
-        return dircache.listdir(dpath)
-            
+        return self.listdir(dpath)